package com.originskyseed.platform.user.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Component;

import com.originskyseed.platform.core.base.BasePage;
import com.originskyseed.platform.core.util.bean.PrimaryKeyGeneratorUtil;
import com.originskyseed.platform.user.mapper.AnchorGroupMapper;
import com.originskyseed.platform.user.mapper.AnchorRelateGroupMapper;
import com.originskyseed.platform.user.model.AnchorGroup;
import com.originskyseed.platform.user.model.AnchorRelateGroup;
import com.originskyseed.platform.user.service.IAnchorGroupService;

/**
 * 分组信息表Service接口实现类（OSI_GROUP）
 *
 * @author WuZhuoXi
 */
@DubboService
@Component("groupService")
public class AnchorGroupServiceImpl implements IAnchorGroupService {

	@Resource
	private AnchorGroupMapper anchorGroupMapper;

	@Resource
	private AnchorRelateGroupMapper anchorRelateGroupMapper;

	/**
	 * 删除记录
	 *
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@Override
	public int deleteByPrimaryKey(String id) throws Exception {
		return this.anchorGroupMapper.deleteByPrimaryKey(id);
	}

	/**
	 * 批量删除记录
	 *
	 * @param ids
	 * @return
	 * @throws Exception
	 */
	@Override
	public int deleteByPrimaryKeys(List<String> ids) throws Exception {
		return this.anchorGroupMapper.deleteByPrimaryKeys(ids);
	}

	/**
	 * 插入记录
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int insert(AnchorGroup record) throws Exception {
		if (StringUtils.isBlank(record.getId())) {
			record.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
		}
		record.setCreateTime(new Date());
		return this.anchorGroupMapper.insert(record);
	}

	/**
	 * 插入记录（仅非空值字段）
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int insertSelective(AnchorGroup record) throws Exception {
		if (StringUtils.isBlank(record.getId())) {
			record.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
		}
		record.setCreateTime(new Date());
		return this.anchorGroupMapper.insertSelective(record);
	}

	/**
	 * 更新记录
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateByPrimaryKey(AnchorGroup record) throws Exception {
		record.setUpdateTime(new Date());
		return this.anchorGroupMapper.updateByPrimaryKey(record);
	}

	/**
	 * 更新记录（仅非空值字段）
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateByPrimaryKeySelective(AnchorGroup record) throws Exception {
		record.setUpdateTime(new Date());
		return this.anchorGroupMapper.updateByPrimaryKeySelective(record);
	}

	/**
	 * 更新置顶信息
	 *
	 * @param anchorGroup
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateIsTop(AnchorGroup anchorGroup) throws Exception {
		anchorGroup.setUpdateTime(new Date());
		this.anchorGroupMapper.updateIsTop(anchorGroup);
		return this.anchorGroupMapper.updateByPrimaryKeySelective(anchorGroup);
	}

	/**
	 * 更新排序信息
	 *
	 * @param downAnchorGroup
	 * @param upAnchorGroup
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updatePrority(AnchorGroup downAnchorGroup, AnchorGroup upAnchorGroup) throws Exception {
		downAnchorGroup.setUpdateTime(new Date());
		upAnchorGroup.setUpdateTime(new Date());
		this.anchorGroupMapper.updateByPrimaryKeySelective(downAnchorGroup);
		return this.anchorGroupMapper.updateByPrimaryKeySelective(upAnchorGroup);
	}

	/**
	 * 根据优先级查询分组
	 * 
	 * @return
	 * @throws Exception
	 */
	@Override
	public AnchorGroup selectGroupByPrority(Integer priority) throws Exception {
		return this.anchorGroupMapper.selectGroupByPriority(priority);
	}

	/**
	 * 查询最大优先级
	 * 
	 * @return
	 * @throws Exception
	 */
	@Override
	public Integer selectPrority() throws Exception {
		return this.anchorGroupMapper.selectPriority();
	}

	/**
	 * 查询记录
	 *
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@Override
	public AnchorGroup selectByPrimaryKey(String id) throws Exception {
		return this.anchorGroupMapper.selectByPrimaryKey(id);
	}

	/**
	 * 查询列表
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<AnchorGroup> selectList(AnchorGroup record) throws Exception {
		return this.anchorGroupMapper.selectList(record);
	}

	/**
	 * 分页查询列表
	 *
	 * @param params
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@Override
	public BasePage<AnchorGroup> selectListByPage(Map<String, Object> params, BasePage<AnchorGroup> page) throws Exception {
		params.put("page", page);
		List<AnchorGroup> list = this.anchorGroupMapper.selectListByPage(params);
		page.setResults(list);
		return page;
	}

	/**
	 * 插入记录（仅非空值字段）
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int insertGroup(AnchorGroup record) throws Exception {
		record.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
		record.setCreateTime(new Date());
		// 插入主播关联分组信息
		insertAnchorList(record);
		return this.anchorGroupMapper.insertSelective(record);
	}

	/**
	 * 更新记录（仅非空值字段）
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateGroup(AnchorGroup record) throws Exception {
		anchorRelateGroupMapper.deleteByGroupId(record.getId());
		// 插入主播关联分组信息
		insertAnchorList(record);
		record.setUpdateTime(new Date());
		return this.anchorGroupMapper.updateByPrimaryKeySelective(record);
	}

	private void insertAnchorList(AnchorGroup record) throws Exception {
		List<AnchorRelateGroup> anchorList = record.getAnchorList();
		if (anchorList != null && anchorList.size() > 0) {
			for (AnchorRelateGroup anchorRelateGroup : anchorList) {
				anchorRelateGroup.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
				anchorRelateGroup.setCreateTime(new Date());
			}
			anchorRelateGroupMapper.insertBatchList(anchorList);
		}
	}
}
